package com.example;

import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class prodectDao {

    public List<prodect> findAll() {
        List<prodect> productList = new ArrayList<>();
        String query = "SELECT * FROM products";
        try {
            ResultSet rs = JdbcUtils.executeQuery(query);
            while (rs.next()) {
                prodect product = new prodect();
                product.setId(rs.getInt("id"));
                product.setName(rs.getString("name"));
                product.setDescription(rs.getString("description"));
                product.setPrice(rs.getDouble("price"));
                productList.add(product);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return productList;
    }

    public boolean deleteProductByID(Integer id){
        String sql = "delete from products where id=?;";
        int flag = JdbcUtils.executeUpdate(sql, id);
        return flag>0;
    }

    public boolean addProduct(prodect product){
        String sql = "insert into products (name, description, price) values (?,?,?);";
        int flag = JdbcUtils.executeUpdate(sql, product.getName(), product.getDescription(),product.getPrice());
        return flag>0;
    }
}
